// pages/merchant/merchant.js
const util = require("../../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */ 
  data: {
    placeholder: '请输入商家、商品名称',
    searchWords: "",
    inputValue:"",
    getSearch: [],
    modalHidden: true,
    historyHidden:false,
    hotSearch:[{"hotitem":"面包熟了"}]
  },
  // input输入框改变
  inputSearch: function (e) {
    this.setData({
      searchWords: e.detail.value,
      inputValue: e.detail.value,
    });
  },
  clickInput: function(){
    wx.navigateBack({})
  },
  // 搜索
  setSearchStorage: function () {
    var that = this;
    //this.setData({ historyHidden:true,})
    let data;
    let localStorageValue = [];
    if (this.data.inputValue != '') {
      //调用API从本地缓存中获取数据
      that.SetSearch(this.data.inputValue);
    } else {
      console.log('空白的')
    }
  },
  goMore:function(e){    
    var that = this;
    var keyword = e.currentTarget.dataset.keyword;
    that.SetSearch(keyword);
    that.GetHotSearchList();  //获取热门搜索
  },
  //删除历史记录
  clearSearchStorage: function () {
    this.setData({
      modalHidden: false,
      historyHidden: false,
    })
  },
  //弹框确定
  modalChangeConfirm:function(){
      wx.setStorageSync('searchData',[])
      this.setData({
        modalHidden:true,
        historyHidden: true,
      })
      
      // this.onLoad();
      
  },
  //弹框取消
  modalChangeCancel:function(){
      this.setData({
        modalHidden:true
      })
  },
  onLoad: function (options) {
    this.GetSearchs();  //显示搜索历史
    this.GetHotSearchList();  //获取热门搜索

  },
  onShow: function () {
    
  },

  onHide: function () {
    
  },
  onUnload: function () {
  
  },

  onPullDownRefresh: function () {
  
  },

  onReachBottom: function () {
  
  },

  onShareAppMessage: function () {
  
  },
  GetSearchs:function(){  //获取本地存储的搜索历史
    var getSearch = wx.getStorageSync('searchData');
    if(getSearch.length>0){     
      this.setData({
        getSearch: getSearch,
        inputValue: '',
        historyHidden:false
      })
    }else{
      this.setData({ historyHidden:true});
    }    
  },
  GetHotSearchList:function(){  //获取热门搜索
    var that = this;
    var data = {};
    data.act = "Find_Hot_search";
    util.Ajax(data, function (json) {
      that.setData({hotSearch:json.data});
    });
  },
  SetSearch:function(keyword){ //保存搜索关键词
    var data = { keyword: keyword};
    data.act = "Save_Hot_search";
    util.Ajax(data, function (json) {
    });
    var searchData = wx.getStorageSync('searchData') || []
    searchData.push(keyword)
    wx.setStorageSync('searchData', searchData);
    
    wx.navigateTo({
      url: '../more/more?keyword=' + keyword
    })
  }
  
})